<!-- @@master  = ../../../tpl/base.html-->
<!-- @@block  =  head-->
<style media="screen">
ul.post-view li{
  text-align: center;
  padding: 10px 20px;
  width: calc(100% / 3 - 10px);
  width: -webkit-calc(100% / 3 - 10px);
  margin: 0px 5px;
  float: left;
}
ul.post-view li:hover, ul.post-view li.active{
  border-radius: 5px;
  color: white;
}
</style>
<!-- @@close-->
<!-- @@block  =  content-->
<section class="panel panel-default">
<header class="panel-heading">
  广告投放
<span class="tools pull-right">
    <a class="fa fa-chevron-down" href="javascript:;"></a>
    <a class="fa fa-times" href="javascript:;"></a>
</span>
</header>
<div class="panel-body">
  <ul class="post-view" style="width:450px">
      <li class="active" data-type="total">所有投放：<span class="badge badge-info"></span></li>
      <li data-type="today">当前投放<span class="badge badge-success"></span></li>
  </ul>
  <div class="table-box">
    <table class="display table table-bordered" id="ad-table"></table>
  </div>
</div>
</section>
<section class="panel panel-default">
<header class="panel-heading">
  明细
<span class="tools pull-right">
    <a class="fa fa-chevron-down" href="javascript:;"></a>
    <a class="fa fa-times" href="javascript:;"></a>
</span>
</header>
<div class="panel-body">
  <div class="table-box">
    <table class="display table table-bordered" id="outersDetail-table"></table>
  </div>
</div>
</section>
<!-- @@close-->
<!-- @@block  =  foot-->
<script type="text/javascript">
(function() {
    "use strict";
    $(document).on("click","#add-ad",function(data){
        window.location.href = "/ad/addAd.html#ads";
    });
    var type = "total"
    $(document).on("click",".post-view >li",function(){
      $(".post-view >li").removeClass("active");
      $(this).addClass("active");
      type = $(this).data("type");
      oTable.fnClearTable();
      if(type == "today"){
        oTable.fnAddData(today_data);
      }else{
        oTable.fnAddData(total_data);
      }
    });
    var oTable = $("#ad-table").dataTable({
      "bSort":false,
      "bProcessing":true,
      "bAutoWidth":false,
      "aoColumns": [
          {"sTitle": "日期","mData": "date"},
          {"sTitle": "应用名称","mData": "app_store_name"},
          {"sTitle": "价格","mData": "price_inner"},
          {"sTitle": "计划跑量","mData": "num_plan"},
          {"sTitle": "实际跑量","mData": "num_result"},
          {"sTitle": "操作","mData": "id"},
      ],
      "aoColumnDefs": [
        {
          "aTargets": [ 2 ],
          "mRender": function ( data, type, full ) {
            return "￥"+(data / 100).toFixed(2);
          }
        }
      ],
      "fnRowCallback":function(nRow,aData,iDataIndex){
          var position = this.fnGetPosition(nRow);
          $('td:eq(-1)',nRow).html('<a href="#" class="detail" data-position="'+position+'">明细</a>    <a href="#" class="export" data-position="'+position+'">导出IDFA</a>');
          return nRow;
      },
    });
    var time = new Date();
    var now  =  myHttp.getDate(time);
    time.setTime(time.getTime()+30*24*3600*1000);
    var date = myHttp.getDate(time);
    var company_id = JSON.parse(sessionStorage.getItem("user")).id;
    var today_data = [];
    var total_data = [];
    myHttp.getJSON("launch/?time_begin=2016-07-20&time_end="+date+"&company_id="+company_id+"&app_id=",function(data){
        oTable.fnClearTable();
        total_data = data;
        for(var i=0;i<data.length;i++){
          var item = data[i];
          if(item.date == (now+" 00:00:00")){
            today_data.push(item);
          }
        }
        $(".badge-info").html(data.length);
        $(".badge-success").html(today_data.length);

        oTable.fnAddData(data);
    });
    var getToday = function(){
      myHttp.getJSON("launch/?time_begin="+now+"&time_end="+now+"&company_id="+company_id+"&app_id=",function(data){
          today_data = data;
          oTable.fnClearTable();
          oTable.fnAddData(today_data);
          $(".badge-success").html(today_data.length);
      });
    }
    setInterval(function(){
      if(type=="today"){
        getToday();
      }

    },5000);
    var dTable = $("#outersDetail-table").dataTable({
      "bSort":false,
      "bProcessing":true,
      "bAutoWidth":false,
      "aoColumns": [
          {"sTitle": "关键词","mData": "keyword"},
          {"sTitle": "排名","mData": "rank_current"},
          {"sTitle": "计划跑量","mData": "num_launch"},
          {"sTitle": "开始时间","mData": "time_begin"},
          {"sTitle": "结束时间","mData": "time_end"},
      ],
    });
    $(document).on("click",".detail",function(){
      var position = $(this).data('position');
      $(this).parents(".panel").find('.fa-chevron-down').click();
      var data  = oTable.fnGetData(position);
      dTable.fnClearTable();
      dTable.fnAddData(data.keywords);
    });
    $(document).on("click",".export",function(){
      var position = $(this).data('position');
      var data  = oTable.fnGetData(position);
      var date = data.date.substr(0,10);
      window.location.href = myHttp.dataUrl+"datas/export_all?app_id="+data.app_id+"&start_time="+date+"&end_time="+date;
    });
})(jQuery);
</script>
<!-- @@close-->
